/*
*

  - @File : response
*/
package utility

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"errors"
	"github.com/gogf/gf/v2/crypto/gmd5"
	"github.com/gogf/gf/v2/crypto/gsha1"
	"github.com/gogf/gf/v2/encoding/gbase64"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/mojocn/base64Captcha"
	"strings"
)

// 判断用户登录状态

func GetOssUrl() string {
	v := ""
	if v == "null" {
		return ""
	}
	return v
}

// 判断元素是否在数组中
func InArray(value string, array []interface{}) bool {
	for _, v := range array {
		if gconv.String(v) == value {
			return true
		}
	}
	return false
}

/*
生成验证码
*/

func CaptchaGenerate() (idKey string, b64s string, err error) {
	// 验证码参数配置：字符,公式,验证码配置
	var configC = base64Captcha.ConfigCharacter{
		Height: 60,
		Width:  240,
		//const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
		Mode:               base64Captcha.CaptchaModeAlphabet,
		ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
		ComplexOfNoiseDot:  base64Captcha.CaptchaComplexLower,
		IsShowHollowLine:   false,
		IsShowNoiseDot:     false,
		IsShowNoiseText:    false,
		IsShowSlimeLine:    false,
		IsShowSineLine:     false,
		CaptchaLen:         4,
	}
	///create a characters captcha.
	idKey, capC := base64Captcha.GenerateCaptcha("", configC)
	//以base64编码
	b64s = base64Captcha.CaptchaWriteToBase64Encoding(capC)
	return

}

func CaptchaVerify(idKey, verifyValue string) bool {
	match := base64Captcha.VerifyCaptcha(idKey, verifyValue)
	return match
}

// ToInt64Array 带分隔符的字符串分裂成int64数组
func ToInt64Array(str, split string) []int64 {
	result := make([]int64, 0)
	if str == "" {
		return result
	}
	arr := strings.Split(str, split)
	if len(arr) > 0 {
		for i := range arr {
			if arr[i] != "" {
				result = append(result, gconv.Int64(arr[i]))
			}
		}
	}
	return result

}

// MD5String MD5哈希值
func MD5String(s string) (str string, err error) {
	str, err = gmd5.EncryptBytes([]byte(s))
	return
}

// SHA1String SHA1哈希值
func SHA1String(s string) (str string) {
	str = gsha1.Encrypt([]byte(s))
	return
}

// 私钥
var privateKey = []byte(`
-----BEGIN RSA PRIVATE KEY-----
MIIBOwIBAAJBAKsOod1ww3FQXDaNX3BowcxrKHKQIqLHFqwYbMwAcluISaPvSnQx
5TW9rDa5h6Zx3K96VKAEPSdBPKCOYt99y2ECAwEAAQJAZwJ2w01mxNQp08gq0xyi
qCFXyDW4ReIyawyCaA9Uzn01ZqKlC0Ewzi4Hs2w+uHqULtgGkGFc5HItCA6c7FA4
4QIhAOHVWbt1On961s2yX7fPjZsADdGnX0o6dVxmjYNAM0CFAiEAwegkH4FH1K5P
+pGO0cSKAsGLyzdiFv3gCiQ19BaB5C0CIF/C5m/0/786yYcj0i7RVgyZ80IppOCG
9WkfG3bCbHk5AiEAuFRRYpGnEwrRhMSyG77V42DR31C6sliMjZd2op5yw9ECIQCF
KbDFGtyqTZ6hWrgJ9KBo0bqTyZRtiUQU/OomGUh4uQ==
-----END RSA PRIVATE KEY-----
`)

// 公钥
var publicKey = []byte(`
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKsOod1ww3FQXDaNX3BowcxrKHKQIqLH
FqwYbMwAcluISaPvSnQx5TW9rDa5h6Zx3K96VKAEPSdBPKCOYt99y2ECAwEAAQ==
-----END PUBLIC KEY-----
`)

// RsaEncrypt 加密
func RsaEncrypt(origData []byte) ([]byte, error) {
	block, _ := pem.Decode(publicKey) //将密钥解析成公钥实例
	if block == nil {
		return nil, errors.New("public key error")
	}
	pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes) //解析pem.Decode（）返回的Block指针实例
	if err != nil {
		return nil, err
	}
	pub := pubInterface.(*rsa.PublicKey)
	var rsaStr []byte
	rsaStr, err = rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
	rsaStr64 := gbase64.Encode(rsaStr)
	return rsaStr64, err //RSA算法加密
}

// RsaDecrypt 解密
func RsaDecrypt(ciphertext64 []byte) ([]byte, error) {
	block, _ := pem.Decode(privateKey) //将密钥解析成私钥实例
	if block == nil {
		return nil, errors.New("private key error!")
	}
	priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) //解析pem.Decode（）返回的Block指针实例
	if err != nil {
		return nil, err
	}
	ciphertext, err := gbase64.Decode(ciphertext64)

	var originData []byte
	originData, err = rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext) //RSA算法解密

	return originData, err
}

func Translator(m *g.MapIntStr, k int) string {
	for i, v := range *m {
		if i == k {
			return v
		}
	}
	return "空"
}
